<?php
class PriceModel extends Model {
    public function __construct() {
        parent::__construct();
        $this->fields = array('id','price_left','price_right');
        $this->tables = array(DB_FREFIX.'price');
        $this->check = new PriceCheck();
        //转义post和get获得的各种参数
        list(
                $this->R['id'],
                $this->R['price_left'],
                $this->R['price_right']
        ) = $this->getRequest()->getParams(
            array(
                isset($_GET['id']) ? $_GET['id'] : null,
                isset($_POST['price_left']) ? $_POST['price_left'] : null,
                isset($_POST['price_right']) ? $_POST['price_right'] : null
            )
        );
    }

    public function findAll() {
        return parent::select($this->tables,$this->fields,array('limit'=>$this->limit,'order'=>'price_left ASC,price_right ASC'));
    }

    public function findOne() {
        $condition = array('id='.$this->R['id']);
        if(!$this->check->oneCheck($this,$condition)) $this->check->displayError();
        return parent::select($this->tables,$this->fields,array('where'=>$condition));
    }

    public function findAddPrice() {
        $allPrice = parent::select($this->tables,$this->fields,array('order'=>'price_left ASC,price_right ASC'));
        foreach($allPrice as $value) {
            $value->value .= $value->price_left.' - '.$value->price_right;
        }
        $allPrice = Tool::setObjectArray($allPrice,'value','value');
        return $allPrice;
    }

    public function add() {
        $condition = array('price_left='.$this->R['price_left'].' AND price_right='.$this->R['price_right']);
        if(!$this->check->addCheck($this,$condition)) $this->check->displayError();
        $needData = $this->getRequest()->filter($this->fields);
        return parent::add($needData);
    }

    public function update() {
        $condition = array('id='.$this->R['id']);
        if(!$this->check->updateCheck($this)) $this->check->displayError();
        $updateData = $this->getRequest()->filter($this->fields);
        return parent::update($updateData,$condition);
    }

    public function delete() {
        $condition = array('id='.$this->R['id']);
        if(!$this->check->oneCheck($this,$condition)) $this->check->displayError();
        return parent::delete($condition);
    }

    public function total() {
        return parent::total();
    }
}

?>